package com.chen;

public class DemoVisibility {
    private  volatile  static boolean flag = true;

    public static void main(String[] args) {
        new Thread(() -> {
            while (flag) {
                // 自旋等待
            }
            //这行代码不会输出
            System.out.println("test");
        }).start();

        try { Thread.sleep(1000); } catch (Exception e) {}
        flag = false;
        System.out.println("Main thread set flag = false");
    }
}
